﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ForumDB
{
	class SubForumQuery : Query
	{
		System.Exception errorSF = new System.Exception("\n\n**OUT OF RANGE**\n");
		IQueryable<SubForum> _subForums;

		public SubForumQuery(IQueryable query)
		{
			_subForums = query.Cast<SubForum>();
		}

		public void setTypeQuery(IQueryable query)
		{
			_subForums = query.Cast<SubForum>();
		}

		public void getInfo()
		{
			foreach (var subForum in _subForums)
			{
				Console.WriteLine("ID={0}, Description={1}", subForum.SubForumID, subForum.Description);
			}
		}


		public String getInfo2()
		{
			String info = "";
			foreach (var subForum in _subForums)
			{
				info = info + "<br/>" + subForum.SubForumID + ", " + subForum.Description + ", " 
				+ subForum.Title + ", " + subForum.Moderator + ", " + subForum.DateTime;
			}
			return info;
		}


		public String getInfo3()
		{
			String info = "";
			foreach (var subForum in _subForums)
			{
				info = info + "33333333 <br/>" + subForum.SubForumID + ", " + subForum.Description + ", "
				+ subForum.Title + ", " + subForum.Moderator + ", " + subForum.DateTime;
			}
			return info;
		}

//*****************************************************************************************************
//*****************************************************************************************************
		public override String getSubForumInfo()
		{
			String info = "";
			foreach (var subForum in _subForums)
			{
				info = info + "<br/>" + subForum.SubForumID + ", " + subForum.Description + ", "
				+ subForum.Title + ", " + subForum.Moderator + ", " + subForum.DateTime;
			}
			return info;
		}

//starting from here:
//*********************** shared in all objects **********************************************************************

		public override int getId(int numberInTable) 
		{
			SubForum tmpSubForum = getValidLine(numberInTable);
			if (tmpSubForum != null)
				return tmpSubForum.SubForumID;
			throw errorSF;
		}

		public override String getTitle(int numberInTable)
		{
			SubForum tmpSubForum = getValidLine(numberInTable);
			if (tmpSubForum != null)
				return tmpSubForum.Title;
			throw errorSF;
		}


//helper methods
		public SubForum getValidLine(int numberInTable)
		{
			if (_subForums.Count() != 0 && numberInTable <= _subForums.Count())
				return _subForums.Skip(numberInTable - 1).Take(1).Single();
			return null;
		}



	}
}
